//
//  RankDetailViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/6.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "RankDetailViewController.h"
#import "NetworkTool.h"
#import "RootRankDetailAttached.h"
#import "RootRankDetailDataModel.h"
#import "RootRankDetailDebuginfoModel.h"
#import "RootRankDetailModel.h"
#import "RootRankDetailNewsimageinfolist.h"
#import "YYAnimationView.h"
@interface RankDetailViewController ()
//<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)UIView *headerView;
//@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)RootRankDetailModel *rootRank;
@property (nonatomic,copy)NSMutableString *str1;
@property (nonatomic,copy)NSMutableString *str2;
@property (nonatomic,copy)NSMutableString *urlString;

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *originLable;
@property (nonatomic,strong)UILabel *timeLabel;

@end

@implementation RankDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.rootRank = [[RootRankDetailModel alloc] init];

//    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 700)];
//    _headerView.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1];
//    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, [UIScreen mainScreen].bounds.size.width - 30, 80)];
//    _titleLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:20];
//    _titleLabel.numberOfLines = 0;
//    [_headerView addSubview:_titleLabel];
    
    
    

//    [_headerView addSubview:_webView];
    
    [self handleData];

//    _tableView.tableHeaderView = _headerView;
//    _tableView.bounces = NO;
//    _webView.scrollView.bounces = NO;
////    _webView.delegate = self;
//    
//    _webView.scrollView.showsVerticalScrollIndicator = NO;
//    [_webView sizeToFit];
//    _webView.delegate = _tableView.tableHeaderView;
//    _webView.scrollView.scrollEnabled = NO;

//    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];

    
}




- (void)handleData{
    
    NSString *urlString = _detailString;
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
//    NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",result);
        
        _rootRank = [RootRankDetailModel modelWithDic:result];
        
        [self.dataArray addObject:_rootRank];
        _webView = [[UIWebView alloc] init];
        _webView.frame =CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 64);
        [self.view addSubview:_webView];
    
        #pragma mark - 1
        
//        NSURL *url = [NSURL URLWithString:_rootRank.data.shareUrlV2];
//        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
//        [_webView loadRequest:request];
//        
//        NSString *htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:_rootRank.data.shareUrlV2] encoding:NSUTF8StringEncoding error:nil];
//        NSLog(@"++====+--=-=-=-=0=0=0-==-==-==0=-=-=-=-=-%@",htmlString);
        
        
        
        #pragma mark - 2
//        NSLog(@"%@",_rootRank.data.newsImageInfoList[0].src);
        _urlString = _rootRank.data.content.mutableCopy;
        
        for (int i = 1; i <= _rootRank.data.newsImageInfoList.count; i ++) {
            
            _str1 = [NSString stringWithFormat:@"${{%d}}$",i].mutableCopy;
            _str2 = _rootRank.data.newsImageInfoList[i - 1].src.mutableCopy;
            NSString *strOne = @"<img width=\"100%\" src=\"";
            NSString *strTwo = @"\"/>";
            NSString *str = [[strOne stringByAppendingString:_str2] stringByAppendingString:strTwo];
         _urlString = [_urlString stringByReplacingOccurrencesOfString:_str1 withString:str].mutableCopy;
        }
        
        [_webView loadHTMLString:_urlString baseURL:nil];
        _titleLabel.text = _rootRank.data.title;
        NSLog(@"fsjflsdjfldsflsd%@",_urlString);
       #pragma mark -
//        [self.tableView reloadData];

        
    } andFailureBlock:^(NSError *error) {
        
    }];

}

- (IBAction)handleBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (IBAction)shareButton:(id)sender {
    [self showAnimationView];
}
- (void)showAnimationView
{
    NSArray *imgNameArray = @[@"朋友圈"
                              ,@"微信"
                              ,@"QQ"
                              ,@"QQ空间"
                              ,@"新浪微博"
                              ,@"复制链接"
                              , @"收藏-1"
                              ,@"字体设置"
                              ,@"举报"
                              ,@"夜间模式"
                              ];
    
    NSArray *titleArray = @[@"朋友圈"
                            ,@"微信"
                            ,@"QQ"
                            ,@"QQ空间"
                            ,@"新浪微博"
                            ,@"复制链接",
                            @"收藏-1",
                            @"字体设置",
                            @"举报",
                            @"夜间模式"
                            ];
    YYAnimationView *animationView = [[YYAnimationView alloc] initImageNameArray:imgNameArray withItmeTitleArray:titleArray];
    animationView.lineItmeNumber = 3;
    animationView.itmeSize = CGSizeMake(100, 100);
    animationView.itmeTitleFont = [UIFont systemFontOfSize:15];
    [animationView addButtonItmeToAnimationView];
    [animationView showAnimationView];
    animationView.yyAnimationButtonItmeClick = ^( NSInteger tags){
        NSLog(@"%ld",(long)tags);
    };
}

#pragma mark -

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//
//    return 20;
//
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
//    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
//    cell.textLabel.text = _rootRank.data.commentLevel;
//    return cell;
//}

#pragma mark -









//- (void)webViewDidFinishLoad:(UIWebView *)webView     //网页加载完成的时候调用
//{
//    CGRect frame = webView.frame;
//    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
//    frame.size = fittingSize;
//    webView.frame = frame;
//    _headerView.frame = frame;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
